package cn.xinfei.xdecision.engine.runner.api;

import cn.xinfei.xdecision.constant.EngineResultEnum;
import cn.xinfei.xdecision.constant.GlobalConstant;
import cn.xinfei.xdecision.engine.runner.context.PipelineContextHolder;
import cn.xinfei.xdecision.engine.runner.executor.DecisionFlowPipeline;
import cn.xinfei.xdecision.engine.runner.executor.NodeHandler;
import cn.xinfei.xdecision.engine.runner.executor.model.StateTransmitModel;
import cn.xinfei.xdecision.engine.runner.vo.EngineApiRequest;
import cn.xinfei.xdecision.model.DecisionApiResponse;
import com.dianping.cat.Cat;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.stereotype.Service;

import java.util.Map;

@Slf4j
@Service
public class DecisionApplyApi extends AbstractDecision<EngineApiRequest, DecisionApiResponse> {

    public void beforeSave() {
        //apply not save
    }

    public void send2Pulsar(long respond) throws Exception {
        //apply not user pulsar
    }

    public DecisionApiResponse run(EngineApiRequest engineApiRequest) {
        Long start = System.currentTimeMillis();
        DecisionApiResponse engineApiResponse = new DecisionApiResponse();
        String engineCode = engineApiRequest.getEngineCode();
        String businessId = engineApiRequest.getRequestId();
        String decisionId = engineApiRequest.getDecisionId();
        engineApiResponse.setDecisionId(decisionId);
        engineApiResponse.setRequestId(businessId);
        engineApiResponse.setEngineCode(engineCode);

        Map<String, Object> fields = engineApiRequest.getIn();
        PipelineContextHolder.setRequestId(businessId);
        PipelineContextHolder.setDecisionId(decisionId);
        PipelineContextHolder.setMainDecisionId(decisionId);
        PipelineContextHolder.setEngineCode(engineCode);
//        PipelineContextHolder.setScene(engineApiRequest.getScene());


        try {

            PipelineContextHolder.putInput(fields);
            MDC.put(GlobalConstant.NID, engineCode);

            NodeHandler first = DecisionFlowPipeline.getFirst();

            String nodeCode = first.getEngineNode().getNodeCode();
            PipelineContextHolder.pushNodeCode(engineCode, nodeCode, decisionId);

            StateTransmitModel stateTransmitModel = StateTransmitModel.build().setEngineResultEnum(EngineResultEnum.APPLY);

            EngineResultEnum resultEnum = stateTransmitModel.getEngineResultEnum();
            Long end = System.currentTimeMillis();
            long respond = end - start;

            PipelineContextHolder.setEngineResult(resultEnum);
            engineApiResponse.setEngineResult(resultEnum);
            engineApiResponse.setRespond(respond);
            return engineApiResponse;

        } catch (Exception e) {
            log.error("apply接口异常,error={}", e.getMessage(), e);
            PipelineContextHolder.setEngineResult(EngineResultEnum.FAIL);
            engineApiResponse.setEngineResult(EngineResultEnum.FAIL);
            engineApiResponse.setMsg("申请受理异常，请重试");
            Cat.logError(e); // 将异常上报到cat上
            return engineApiResponse;
        }
    }


}
